package org.jeecg.modules.yusuan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.system.entity.SysCategory;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.mapper.SysCategoryMapper;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.system.model.TreeSelectModel;
import org.jeecg.modules.system.service.ISysCategoryService;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.yusuan.entity.CaiwuBiaozhun;
import org.jeecg.modules.yusuan.entity.CaiwuLiuyong;
import org.jeecg.modules.yusuan.entity.NianDuHuiZong;
import org.jeecg.modules.yusuan.mapper.*;
import org.jeecg.modules.yusuan.service.ICaiwuBiaozhunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: caiwu_yusuan
 * @Author: jeecg-boot
 * @Date:   2020-06-15
 * @Version: V1.0
 */
@Service
public class CaiwuBiaozhunServiceImpl extends ServiceImpl<CaiwuBiaozhunMapper, CaiwuBiaozhun> implements ICaiwuBiaozhunService {

    @Autowired
    private SysDepartMapper sysDepartMapper;
    @Autowired
    private SysCategoryMapper sysCategoryMapper;
    @Autowired
    private CaiwuLiuyongMapper caiwuLiuyongMapper;
    @Autowired
    private CaiwuBiaozhunMapper caiwuBiaozhunMapper;
    @Autowired
    private CaiwuXiangmuMapper caiwuXiangmuMapper;
    @Autowired
    private CaiwuZhijieMapper caiwuZhijieMapper;
    @Autowired
    private CaiwuShangnianMapper caiwuShangnianMapper;


    @Override
    public List<CaiwuBiaozhun> listData(String deptId,String deptCode, String pid,Integer effective,Integer version) {
        return this.baseMapper.listData(deptId,deptCode,pid,effective,version);
    }

    @Override
    public List<CaiwuBiaozhun> zhibiaolistData(String deptId,String deptCode, String pid,Integer effective,Integer version) {
        return this.baseMapper.zhibiaolistData(deptId,deptCode,pid,effective,version);
    }

    @Override
    public List<NianDuHuiZong> nianduhuizong(String orgCode) {
        List<NianDuHuiZong> retList = new ArrayList<>();
        Map<String,NianDuHuiZong> map = new HashMap<>();
        String deptId = this.sysDepartMapper.selectOne(new LambdaQueryWrapper<SysDepart>().eq(SysDepart::getOrgCode,orgCode)).getId();
        List<SysDepart> departList = this.sysDepartMapper.getSubOrgCodeByPcode(orgCode);

        List<SysCategory> categoryList = this.sysCategoryMapper.selectList(new LambdaQueryWrapper<SysCategory>().eq(SysCategory::getSysOrgCode,"A01").eq(SysCategory::getPid,"1270528547133616130").orderByAsc(SysCategory::getSeq));

        if(CollectionUtils.isNotEmpty(categoryList) ){
            categoryList.forEach(sysCategory -> {
                NianDuHuiZong nianDuHuiZong = new NianDuHuiZong();
                nianDuHuiZong.setCategoryId(sysCategory.getId());
                nianDuHuiZong.setParentId(sysCategory.getPid());
                nianDuHuiZong.setCategoryName(sysCategory.getName());
                nianDuHuiZong.setIsLeaf(null == sysCategory.getHasChild());
                retList.add(nianDuHuiZong);
                map.put(nianDuHuiZong.getCategoryId(),nianDuHuiZong);
                loadAllCategoryChildren(sysCategory,nianDuHuiZong,map);
            });

            //收入
            List<Map> biaozhunList = this.caiwuBiaozhunMapper.shenheshu(deptId);
            List<Map> xiangmuList = this.caiwuXiangmuMapper.shenheshu(deptId);
            List<Map> liuyongList = this.caiwuLiuyongMapper.shenheshu(deptId);
            List<Map> shangnianList = this.caiwuShangnianMapper.shenheshu(deptId);

            list2Niandu(biaozhunList,"biaozhun",map);
            list2Niandu(xiangmuList,"xiangmu",map);
            list2Niandu(liuyongList,"liuyong",map);
            list2Niandu(shangnianList,"shangnian",map);

            //支出
            List<Map> zhijieList = this.caiwuZhijieMapper.shenheshu(deptId);
            list2Niandu(zhijieList,"zhijie",map);

            if(CollectionUtils.isEmpty(departList))
                return retList;

            List<Map> dxBiaozhunList = this.caiwuBiaozhunMapper.duixiaShenheshu(departList);
            List<Map> dxXiangmuList = this.caiwuXiangmuMapper.duixiaShenheshu(departList);
            list2Niandu(dxBiaozhunList,"dxbiaozhun",map);
            list2Niandu(dxXiangmuList,"dxxiangmu",map);
        }


        return retList;
    }

    private void loadAllCategoryChildren(SysCategory category,NianDuHuiZong nianDuHuiZong,Map<String,NianDuHuiZong> map) {
        if(null != category) {
            List<SysCategory> categoryList = this.sysCategoryMapper.selectList(new LambdaQueryWrapper<SysCategory>().eq(SysCategory::getSysOrgCode,"A01").eq(SysCategory::getPid, category.getId()).orderByAsc(SysCategory::getSeq));
            if(CollectionUtils.isNotEmpty(categoryList)){
                List<NianDuHuiZong> tmpList = new ArrayList<>();
                nianDuHuiZong.setChildren(tmpList);
                categoryList.forEach(sysCategory -> {
                    NianDuHuiZong tmp = new NianDuHuiZong();
                    tmp.setCategoryId(sysCategory.getId());
                    tmp.setParentId(sysCategory.getPid());
                    tmp.setCategoryName(sysCategory.getName());
                    tmp.setIsLeaf(null == sysCategory.getHasChild());
                    tmpList.add(tmp);
                    map.put(tmp.getCategoryId(),tmp);
                    loadAllCategoryChildren(sysCategory,tmp,map);
                });
            }
        }
    }

    private void list2Niandu(List<Map> list,String type,Map<String,NianDuHuiZong> map){
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(item->{
                String shenheshu = (null == item.get("shenheshu")?"0":item.get("shenheshu").toString());
                String jz = (null == item.get("jz")?"0":item.get("jz").toString());
                String categoryId = (String)item.get("categoryId");
                NianDuHuiZong nianDuHuiZong = map.get(categoryId);
                if(StringUtils.equals("biaozhun",type)) {
                    nianDuHuiZong.setSrBiaozhun(shenheshu);
                    nianDuHuiZong.setJzbiaozhun(jz);
                }
                if(StringUtils.equals("xiangmu",type)) {
                    nianDuHuiZong.setSrXiangmu(shenheshu);
                    nianDuHuiZong.setJzxiangmu(jz);
                }
                if(StringUtils.equals("liuyong",type)) {
                    nianDuHuiZong.setSrLiuyong(shenheshu);
                    nianDuHuiZong.setJzliuyong(jz);
                }
                if(StringUtils.equals("shangnian",type)) {
                    nianDuHuiZong.setSrShangnian(shenheshu);
                    nianDuHuiZong.setJzshangnian(jz);
                }
                if(StringUtils.equals("zhijie",type))
                    nianDuHuiZong.setZcZhijie(shenheshu);
                if(StringUtils.equals("dxbiaozhun",type))
                    nianDuHuiZong.setZcBiaozhun(shenheshu);
                if(StringUtils.equals("dxxiangmu",type))
                    nianDuHuiZong.setZcXiangmu(shenheshu);
            });
        }
    }

    @Override
    public Map<String,NianDuHuiZong> nianduhuizongData(List<NianDuHuiZong> list) {

        Map<String,NianDuHuiZong> allData = new HashMap<>();
        List<NianDuHuiZong> leafList = new ArrayList<>();
        iterChildren(list,allData,leafList);

        if(!org.springframework.util.CollectionUtils.isEmpty(leafList)){
            leafList.forEach(item->{
                String srBiaozhun = item.getSrBiaozhun();
                String srLiuyong = item.getSrLiuyong();
                String srShangnian = item.getSrShangnian();
                String srXiangmu = item.getSrXiangmu();

                srBiaozhun = null == srBiaozhun?"0":srBiaozhun;
                srLiuyong = null == srLiuyong?"0":srLiuyong;
                srShangnian = null == srShangnian?"0":srShangnian;
                srXiangmu = null == srXiangmu?"0":srXiangmu;

                String zcBiaozhun = item.getZcBiaozhun();
                String zcXiangmu = item.getZcXiangmu();
                String zcZhijie = item.getZcZhijie();
                zcBiaozhun = null == zcBiaozhun?"0":zcBiaozhun;
                zcXiangmu = null == zcXiangmu?"0":zcXiangmu;
                zcZhijie = null == zcZhijie?"0":zcZhijie;

                item.setSrHeji((Double.parseDouble(srBiaozhun) + Double.parseDouble(srLiuyong) + Double.parseDouble(srShangnian) + Double.parseDouble(srXiangmu)) + "");
                item.setZcHeji((Double.parseDouble(zcBiaozhun) + Double.parseDouble(zcXiangmu) + Double.parseDouble(zcZhijie)) + "");
                if(Double.parseDouble(srBiaozhun) + Double.parseDouble(srLiuyong) + Double.parseDouble(srShangnian) + Double.parseDouble(srXiangmu)
                        + Double.parseDouble(zcBiaozhun) + Double.parseDouble(zcXiangmu) + Double.parseDouble(zcZhijie) > 0){
                    item.setDisplay(true);
                    iterNdParent(allData,item,item);
                }
            });
        }
        return allData;
    }


    private void iterNdParent(Map<String,NianDuHuiZong> allData,NianDuHuiZong nianDuHuiZong,NianDuHuiZong leafItem){
        String categoryId = nianDuHuiZong.getParentId();
        NianDuHuiZong parent =allData.get(categoryId);
        if(null == parent)
            return;
        parent.setDisplay(true);
        if(!org.apache.commons.lang3.StringUtils.equals(parent.getCategoryId(),"1270528547133616130")){
            String xiafashu = parent.getSrBiaozhun();
            xiafashu = null == xiafashu ? "0" : xiafashu;
            String xfs = leafItem.getSrBiaozhun();
            xfs = null == xfs ? "0" : xfs;
            parent.setSrBiaozhun((Double.parseDouble(xiafashu) + Double.parseDouble(xfs)) + "");

            String shenqingshu = parent.getSrXiangmu();
            shenqingshu = null == shenqingshu ? "0" : shenqingshu;
            String sqs = leafItem.getSrXiangmu();
            sqs = null == sqs ? "0" : sqs;
            parent.setSrXiangmu((Double.parseDouble(shenqingshu) + Double.parseDouble(sqs)) + "");

            String shenheshu = parent.getSrLiuyong();
            shenheshu = null == shenheshu ? "0" : shenheshu;
            String shs = leafItem.getSrLiuyong();
            shs = null == shs ? "0" : shs;
            parent.setSrLiuyong((Double.parseDouble(shenheshu) + Double.parseDouble(shs)) + "");

            String zengjian = parent.getSrShangnian();
            zengjian = null == zengjian?"0":zengjian;
            String zj = leafItem.getSrShangnian();
            zj = null == zj?"0":zj;
            parent.setSrShangnian((Double.parseDouble(zengjian) + Double.parseDouble(zj)) + "");

            String zcBiaozhun = parent.getZcBiaozhun();
            zcBiaozhun = null == zcBiaozhun ? "0" : zcBiaozhun;
            String zcbz = leafItem.getZcBiaozhun();
            zcbz = null == zcbz ? "0" : zcbz;
            parent.setZcBiaozhun((Double.parseDouble(zcBiaozhun) + Double.parseDouble(zcbz)) + "");

            String zcXiangmu = parent.getZcXiangmu();
            zcXiangmu = null == zcXiangmu ? "0" : zcXiangmu;
            String zcxm = leafItem.getZcXiangmu();
            zcxm = null == zcxm ? "0" : zcxm;
            parent.setZcXiangmu((Double.parseDouble(zcXiangmu) + Double.parseDouble(zcxm)) + "");

            String zcZhijie = parent.getZcZhijie();
            zcZhijie = null == zcZhijie ? "0" : zcZhijie;
            String zczj = leafItem.getZcZhijie();
            zczj = null == zczj ? "0" : zczj;
            parent.setZcZhijie((Double.parseDouble(zcZhijie) + Double.parseDouble(zczj)) + "");

            String jiezhuan = parent.getJiezhuan();
            jiezhuan = null == jiezhuan ? "0" : jiezhuan;
            String jzxn = leafItem.getJiezhuan();
            jzxn = null == jzxn ? "0" : jzxn;
            parent.setJiezhuan((Double.parseDouble(jiezhuan) + Double.parseDouble(jzxn)) + "");

            String srHeji = parent.getSrHeji();
            srHeji = null == srHeji? "0": srHeji;
            String srhj = leafItem.getSrHeji();
            srhj = null == srhj? "0": srhj;
            parent.setSrHeji((Double.parseDouble(srHeji) + Double.parseDouble(srhj)) + "");

            String zcHeji = parent.getZcHeji();
            zcHeji = null == zcHeji? "0": zcHeji;
            String zchj = leafItem.getZcHeji();
            zchj = null == zchj? "0": zchj;
            parent.setZcHeji((Double.parseDouble(zcHeji) + Double.parseDouble(zchj)) + "");

            iterNdParent(allData,parent,leafItem);
        }
    }

    private void iterChildren(List<NianDuHuiZong> list,Map<String,NianDuHuiZong> allData,List<NianDuHuiZong> leafList){
        if(null != list && list.size()>0){
            list.forEach(item->{
                allData.put(item.getCategoryId(),item);
                if(!org.springframework.util.CollectionUtils.isEmpty(item.getChildren())){
                    iterChildren(item.getChildren(),allData,leafList);
                }else{
                    leafList.add(item);
                }
            });
        }
    }

    @Override
    public Integer getMaxVersion(Integer effective, String deptId) {
        return this.caiwuBiaozhunMapper.getMaxVersion(effective,deptId);
    }
}
